﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using System.IO;

namespace World
{
    public partial class World : System.Web.UI.Page
    {

        LinkButton linkButton;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                SelectCountriesForSelectedContinent();
            }
            

        }

        protected void ContinentsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.CountriesGridView.Visible = true;
            this.TownsListView.Visible = false;
            SelectCountriesForSelectedContinent();
        }

        private void SelectCountriesForSelectedContinent()
        {
            this.CountriesDataScource.Where = "it.ContinentID = " + this.ContinentsListBox.SelectedValue;
            
        }

        protected void CountriesGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.TownsListView.Visible = true;
            this.TownsDataSource.Where = "it.CountryID = " + this.CountriesGridView.SelectedValue;
        }

        protected void CountriesGridView_PreRender(object sender, EventArgs e)
        {
            this.CountriesGridView.FooterRow.Cells[0].Controls.Add(linkButton);
        }

        void linkButton_Click(object sender, EventArgs e)
        {
            string countryName = ((TextBox)this.CountriesGridView.FooterRow.FindControl("CountryNameTB")).Text;
            string language= ((TextBox)this.CountriesGridView.FooterRow.FindControl("CountryLanguageTB")).Text;
            int population= int.Parse(((TextBox)this.CountriesGridView.FooterRow.FindControl("CountryPopulationTB")).Text);
            int continentID = int.Parse(this.ContinentsListBox.SelectedValue);
            FileUpload fileUpload = (FileUpload)this.CountriesGridView.FooterRow.FindControl("FileUploadFlagFooter");

            WorldEntities1 worldEntities = new WorldEntities1();
            Country newCountry = new Country()
            {
                 Name = countryName,
                Language = language,
                Population = population,
                ContinentID = continentID,
                Flag = this.ReadBytesFromUploadControl(fileUpload)
            };
            worldEntities.Countries.AddObject(newCountry);
            worldEntities.SaveChanges();

           
            SelectCountriesForSelectedContinent();

        }

        protected void CountriesGridView_Init(object sender, EventArgs e)
        {
            
        }

        protected void CountriesGridView_Load(object sender, EventArgs e)
        {
            linkButton = new LinkButton(); linkButton.Text = "Insert";
            linkButton.ID = "InsertButton";
            linkButton.Click += new EventHandler(linkButton_Click);
            this.CountriesGridView.FooterRow.Cells[0].Controls.Add(linkButton);
        }

        protected void TownsListView_ItemInserted(object sender, ListViewInsertedEventArgs e)
        {
            this.TownsDataSource.Where = "it.CountryID = " + this.CountriesGridView.SelectedValue;
        }

        protected void TownsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            
        }

        protected void TownsListView_ItemCreated(object sender, ListViewItemEventArgs e)
        {
            ListViewItem currentItem = e.Item;
            Label tb = currentItem.FindControl("CountryIDTextBox") as Label;
            if (tb == null)
            {
                return;
            }
            tb.Text = this.CountriesGridView.SelectedValue.ToString();
        }

        protected void CountriesGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
           FileUpload fileUpload=(FileUpload) this.CountriesGridView.Rows[e.RowIndex].FindControl("FileUploadFlag");
           byte[] bytesRead= ReadBytesFromUploadControl(fileUpload);
           int countryID = int.Parse(this.CountriesGridView.Rows[e.RowIndex].Cells[1].Text);
           this.SaveImageToDataBase(bytesRead, countryID);
        }
  
        private byte[] ReadBytesFromUploadControl(FileUpload fileUpload)
        {
            BinaryReader reader = new BinaryReader(fileUpload.PostedFile.InputStream);

            using (reader)
            {
                byte[] bytesRead = reader.ReadBytes((int)reader.BaseStream.Length);
                return bytesRead;
            }
        }

        private void SaveImageToDataBase(byte[] image, int countryID)
        {
            WorldEntities1 worldEntities = new WorldEntities1();

            Country country = worldEntities.Countries.First(c => c.CountryID == countryID);
            country.Flag = image;

            worldEntities.SaveChanges();
        }
    }
}